package com.warm.mini.wework.service.invoke.handler;

import com.alibaba.fastjson.JSON;
import com.warm.mini.wework.config.WeworkConfigProperties;
import com.warm.mini.wework.constant.WeworkConst;
import com.warm.mini.wework.dao.WeworkExternalUserDaoImpl;
import com.warm.mini.wework.dao.WeworkUserDaoImpl;
import com.warm.mini.wework.entity.invoke.BaseEventMsg;
import com.warm.mini.wework.util.JaxbUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description 通用消息处理
 * @Author warmchuang
 * @Date 2022/2/9 16:22
 */
@Slf4j
@Service
public class BaseMsgHandler extends AbstractMsgHandler{

    public static final String BASE_RESOURCE = "common";

    @Resource
    private WeworkUserDaoImpl weworkUserDao;
    @Resource
    private WeworkExternalUserDaoImpl weworkExternalUserDao;

    @Override
    public String resource() {
        return BASE_RESOURCE;
    }

    @Override
    public void changeContactHandle(String xmlMsg, WeworkConfigProperties config) {
        BaseEventMsg event = JaxbUtil.xml2Bean(xmlMsg, BaseEventMsg.class);
        log.info(">>> changeContactHandle event:[{}]", JSON.toJSONString(event));
        weworkUserDao.save();

        WeworkConst.ChangeContactEvent eventEvent = WeworkConst.ChangeContactEvent.getByName(event.getChangeType());
        // todo 自定义对应事件的实体类型进行处理
        switch (eventEvent){
            case CREATE_USER:
                break;
            case UPDATE_USER:
                break;
            case DELETE_USER:
                break;
            default:
                log.info("change contact unknown eventEnum");
                break;
        }
    }

    @Override
    public void changeExternalContactHandle(String xmlMsg, WeworkConfigProperties config) {
        BaseEventMsg event = JaxbUtil.xml2Bean(xmlMsg, BaseEventMsg.class);
        log.info(">>> changeExternalContactHandle event:[{}]", JSON.toJSONString(event));
        WeworkConst.ChangeExternalContactEvent eventEvent = WeworkConst.ChangeExternalContactEvent.getByName(event.getChangeType());
        weworkExternalUserDao.save();
        switch (eventEvent){
            case ADD_EXTERNAL_CONTACT:
                break;
            case EDIT_EXTERNAL_CONTACT:
                break;
            default:
                log.info("change external contact unknown eventEnum");
                break;
        }
    }
}
